#include <vector>
#include "missilecommand.h"
#include "siloactor.h"
#include "gameaction.h"
#include "gametrigger.h"

using std::vector;

extern Game *game;

GameTrigger::GameTrigger(GameAction *action)
  : action(action) {};

GameTrigger::~GameTrigger()
{
  delete action;
}

void GameTrigger::setAction(GameAction *action)
{
  this->action = action;
}

NoAmmoTrigger::NoAmmoTrigger(GameAction *action)
  : GameTrigger(action) {};

bool NoAmmoTrigger::test()
{
  set<SiloActor*>::iterator it;
  for(it = game->silos.begin(); it != game->silos.end(); ++it) {
    SiloActor *silo = *it;
    if(silo->getHealthPct() > 0.0 && silo->getAmmoPct() > 0.0)
      return false;
  }
  
  action->perform();
  return true;
}

NoSilosTrigger::NoSilosTrigger(GameAction *action)
  : GameTrigger(action) {};

bool NoSilosTrigger::test()
{
  set<SiloActor*>::iterator it;
  for(it = game->silos.begin(); it != game->silos.end(); ++it) {
    SiloActor *silo = *it;
    if(silo->getHealthPct() > 0.0)
      return false;
  }
  
  action->perform();
  return true;
}

TimerUpTrigger::TimerUpTrigger(GameAction *action)
  : GameTrigger(action) {};

bool TimerUpTrigger::test()
{
  if(game->t < game->timerend)
    return false;
  
  action->perform();
  return true;
}
